package ext;

import org.junit.Test;

public class BaiduX1 {

    
    //六个数字得到最大时间
    /* 
        基于贪心思想：按照时分秒顺序每次找到满足条件的最大时间
        时：0,1,2，若为2次位小于5
    */
    public String maxTime(int[] nums) {
        int hh = findMax(nums, 2), hl = 0; 

        //在判断首位而得到不正数的情况下，应该直接返回“invalid”，懒得写
        //这题默认将24:00：00视为0点，即最小值
        if (hh == 2) {
            hl = findMax(nums, 3);
        } else {
            hl = findMax(nums, 9);
        }
        int mh = findMax(nums, 5);
        int ml = findMax(nums, 9);

        int sh = findMax(nums, 5);
        int sl = findMax(nums, 9);

        return hh + "" + hl + ":" + mh + "" + ml + ":"  + sh + "" + sl;
    }

    //找到小于等于target且没被使用的最大值
    public int findMax(int[] nums, int target) {
        int res = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != -1 && nums[i] <= target) {
                if (res < 0 || nums[res] < nums[i]) res = i;
            }
        }

        if (res == -1) return -1;

        //使用过的值置为-1
        int tmp = nums[res];
        nums[res] = -1;
        return tmp;
    }


    //六个数字得到最小时间
    //应该反着来就行
    /* 
        从秒第二位开始，每次找较大值
    */
    public String minTime(int[] nums) {

        //每次找xh的时候失败就应该直接返回
        int sl = findMax(nums, 9);
        int sh = findMax(nums, 5);

        int ml = findMax(nums, 9);
        int mh = findMax(nums, 5);

        int hl = findMax(nums, 9);
        int hh = findMax(nums, 2);
        if (hh == 2 && hl >= 4) return "invalid";

        return hh + "" + hl + ":" + mh + "" + ml + ":"  + sh + "" + sl;
    }

    @Test
    public void test() {
        int[] nums = new int[]{9,2,3,0,5,6};
        System.out.println(maxTime(nums));
        nums = new int[]{9,2,3,0,5,6};
        System.out.println(minTime(nums));
    }
}
